/*
 * Javlov - a Java toolkit for reinforcement learning with multi-agent support.
 * 
 * Copyright (c) 2009-2011 Matthijs Snel
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package net.javlov;

import java.util.Map;
import java.util.HashMap;

public class StateVisitStatistic implements Statistic {

	protected Map<State, Integer> stateCount;
	protected int totalCount;
	
	public StateVisitStatistic(int size) {
		stateCount = new HashMap<State, Integer>(size*2);
	}
	
	@Override
	protected void finalize() {
		System.out.println(this);
	}
	
	@Override
	public void init() {
		stateCount.clear();
		totalCount = 0;		
	}

	@Override
	public void reset() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void update(State s, Action a, double reward) {
		totalCount++;
		Integer c = stateCount.get(s);
		if ( c == null ) {
			stateCount.put(s, 1);
			return;
		}
		stateCount.put(s, c+1);
	}
	
	public Map<State, Integer> getStateVisits() {
		return stateCount;
	}
	
	public double getPct(State s) {
		Integer c = stateCount.get(s);
		if ( c == null )
			return -1;
		return c / totalCount;
	}
	
	@Override
	public String toString() {
		String s = "";
		for ( Map.Entry<State, Integer> e : stateCount.entrySet() )
			s += e.getKey() + ":" + e.getValue() + "\n";
		return s;
	}

}
